﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Rename
{
    public partial class Form4 : Form
    {
        private ListView listView1;
        private ListViewItem draggedItem = null;
        private int dragIndex = -1;
        public Form4()
        {
            InitializeComponent();

            // 初始化 ListView
            listView1 = new ListView();
            listView1.Dock = DockStyle.Fill;
            listView1.View = View.Details;
            listView1.AllowDrop = true; // 允许拖放
            listView1.FullRowSelect = true;
            listView1.GridLines = true;

            // 添加列
            listView1.Columns.Add("序号", 60);
            listView1.Columns.Add("项目名称", 200);
            listView1.Columns.Add("描述", 300);

            // 添加示例数据
            for (int i = 0; i < 8; i++)
            {
                listView1.Items.Add(new ListViewItem(new[]
                {
                (i + 1).ToString(),
                $"项目 {i + 1}",
                $"这是第 {i + 1} 个示例项目"
            }));
            }

            // 绑定拖拽相关事件
            listView1.ItemDrag += ListView1_ItemDrag;       // 开始拖拽
            listView1.DragEnter += ListView1_DragEnter;     // 拖拽进入
            listView1.DragOver += ListView1_DragOver;       // 拖拽过程
            listView1.DragDrop += ListView1_DragDrop;       // 完成拖拽
            listView1.MouseMove += ListView1_MouseMove;     // 鼠标移动

            this.Controls.Add(listView1);

            this.Text = "支持拖拽的 ListView";
            this.Size = new Size(600, 400);
        }
        // 开始拖拽
        private void ListView1_ItemDrag(object sender, ItemDragEventArgs e)
        {
            if (e.Item is ListViewItem item)
            {
                draggedItem = item;
                dragIndex = item.Index;
                // 开始拖拽操作
                DoDragDrop(item, DragDropEffects.Move);
            }
        }
        // 拖拽进入控件
        private void ListView1_DragEnter(object sender, DragEventArgs e)
        {
            // 验证拖拽的数据格式
            if (e.Data.GetDataPresent(typeof(ListViewItem)))
            {
                e.Effect = DragDropEffects.Move; // 允许移动操作
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }

        // 拖拽过程中
        private void ListView1_DragOver(object sender, DragEventArgs e)
        {
            // 获取鼠标位置并确定目标索引
            Point point = listView1.PointToClient(new Point(e.X, e.Y));
            ListViewItem targetItem = listView1.GetItemAt(point.X, point.Y);

            // 高亮显示拖拽目标位置
            if (targetItem != null && targetItem != draggedItem)
            {
                e.Effect = DragDropEffects.Move;
            }
        }

        // 鼠标移动（用于显示拖拽提示线）
        private void ListView1_MouseMove(object sender, MouseEventArgs e)
        {
            if (draggedItem != null)
            {
                listView1.Invalidate(); // 重绘以显示提示线
            }
        }
        // 完成拖拽
        private void ListView1_DragDrop(object sender, DragEventArgs e)
        {
            if (draggedItem == null) return;

            // 获取放置位置
            Point point = listView1.PointToClient(new Point(e.X, e.Y));
            ListViewItem targetItem = listView1.GetItemAt(point.X, point.Y);

            if (targetItem != null && targetItem != draggedItem)
            {
                // 保存被拖拽项的数据
                string[] itemData = new string[draggedItem.SubItems.Count];
                for (int i = 0; i < draggedItem.SubItems.Count; i++)
                {
                    itemData[i] = draggedItem.SubItems[i].Text;
                }

                // 移除原项
                int originalIndex = draggedItem.Index;
                listView1.Items.Remove(draggedItem);

                // 插入到新位置
                int targetIndex = targetItem.Index;
                // 如果目标位置在原位置之后，需要调整索引（因为原项已被移除）
                if (targetIndex > originalIndex)
                {
                    targetIndex--;
                }

                ListViewItem newItem = new ListViewItem(itemData);
                listView1.Items.Insert(targetIndex, newItem);

                // 选中新插入的项
                newItem.Selected = true;
                listView1.Focus();
            }

            // 重置拖拽状态
            draggedItem = null;
            dragIndex = -1;
            listView1.Invalidate();
        }
    }
}
